﻿namespace AsyncHomework;

class Program
{
    static async Task Main(string[] args)
    {
        Console.WriteLine(await GetHtmlString(@"https://www.taobao.com"));
        Console.ReadKey();
    }


    static async Task<string> GetHtmlString(string url)
    {
        int tryCount = 3;
        string result = string.Empty;
        for (int i = 0; i < tryCount; i++)
        {
            try
            {
                using (HttpClient hc = new HttpClient())
                {
                    result = await hc.GetStringAsync(url);
                    return result;
                }
            }
            catch (Exception e) when (i < tryCount-1)
            {
                Console.WriteLine("下载失败，等待再次尝试...");
                await Task.Delay(500);
            }
        }

        throw new InvalidOperationException("下载失败");
    }
}